'''
Created on Feb 28, 2014

@author: dan
'''

from nltk.corpus import wordnet
from nltk.tokenize.punkt import PunktWordTokenizer
import sys


functionwords = ['about', 'across', 'against', 'along', 'around', 'at',
                 'behind', 'beside', 'besides', 'by', 'despite', 'down',
                 'during', 'for', 'from', 'in', 'inside', 'into', 'near', 'of',
                 'off', 'on', 'onto', 'over', 'through', 'to', 'toward',
                 'with', 'within', 'without', 'anything', 'everything',
                 'anyone', 'everyone', 'ones', 'such', 'it', 'itself',
                 'something', 'nothing', 'someone', 'the', 'some', 'this',
                 'that', 'every', 'all', 'both', 'one', 'first', 'other',
                 'next', 'many', 'much', 'more', 'most', 'several', 'no', 'a',
                 'an', 'any', 'each', 'no', 'half', 'twice', 'two', 'second',
                 'another', 'last', 'few', 'little', 'less', 'least', 'own',
                 'and', 'but', 'after', 'when', 'as', 'because', 'if', 'what',
                 'where', 'which', 'how', 'than', 'or', 'so', 'before', 'since',
                 'while', 'although', 'though', 'who', 'whose', 'can', 'may',
                 'will', 'shall', 'could', 'be', 'do', 'have', 'might', 'would',
                 'should', 'must', 'here', 'there', 'now', 'then', 'always',
                 'never', 'sometimes', 'usually', 'often', 'therefore',
                 'however', 'besides', 'moreover', 'though', 'otherwise',
                 'else', 'instead', 'anyway', 'incidentally', 'meanwhile']

def overlapsenses( synset1, synset2 ):
    gloss1 = set(PunktWordTokenizer().tokenize(synset1.definition))
    gloss2 = set(PunktWordTokenizer().tokenize(synset2.definition))
    gloss1 = gloss1.difference(functionwords)
    gloss2 = gloss2.difference(functionwords)
    return len(gloss1.intersection(gloss2))
    
def overlapcontext( synset, sentence ):
    gloss = set(PunktWordTokenizer().tokenize(synset.definition))
    gloss = gloss.difference( functionwords )
    if isinstance(sentence, str):
        sentence = set(PunktWordTokenizer().tokenize(sentence.definition))
    elif isinstance(sentence, list):
        sentence = set(sentence)
    elif isinstance(sentence, set):
        pass
    else:
        return
    sentence = sentence.difference( functionwords )
    return len( gloss.intersection(sentence) )

def elesksim( synset1, synset2 ):
    hypons1 = [h for h in synset1.hyponyms()]
    hypons2 = [h for h in synset2.hyponyms()]
    elesk = overlapsenses(synset1, synset2)
    for hypon1 in hypons1:
        elesk += overlapsenses(hypon1, synset2)
        for hypon2 in hypons2:
            elesk += overlapsenses( hypon1, hypon2 )
    for hypon2 in hypons2:
        elesk += overlapsenses( synset1, hypon2 )
    return elesk

def simplesk( word, sentence ):
    bestsense = None
    maxoverlap = 0
    for sense in wordnet.synsets(word):
        overlap = overlapcontext(sense,sentence)
        for h in sense.hyponyms():
            overlap += overlapcontext( h, sentence )
        if overlap > maxoverlap:
                maxoverlap = overlap
                bestsense = sense
    return bestsense

def elesk( word1, word2 ):
    best = (0,None,None)
    for sense1 in wordnet.synsets(word1):
        for sense2 in wordnet.synsets(word2):
            sim = elesksim( sense1, sense2 )
            if sim > best[0]:
                best = (sim, sense1, sense2)
    return best

if __name__ == '__main__':
    infile = open(sys.argv[1])
    good = 0
    bad = 0
    for line in infile:
            line = line.split(' ')  # take the input apart and assign          
            sentence = {"id"        : line[0],
                        "verb"      : line[1],
                        "noun1"     : line[2],
                        "noun2"     : line[4],
                        "stdanswer" : line[5].rstrip()}
            if sys.argv[2] == 'wsd':
                slesk = simplesk( sentence['noun2'], \
                          [sentence['verb'], sentence['noun1']] )
                print line
                if slesk:
                    print slesk.definition
                else:
                    print "I got nothing"
                slesk = simplesk( sentence['noun1'], \
                          [sentence['verb'], sentence['noun2']] )
                print line
                if slesk:
                    print slesk.definition
                else:
                    print "I got nothing"
            elif sys.argv[2] == 'sim':
                e = elesk(sentence["noun1"], sentence["noun2"])
                print line
                try:
                    if sentence["stdanswer"] == 'N':
                        good += 1
                    else:
                        bad += 1
                    print e[1].definition, len(wordnet.synsets(sentence["noun1"]))
                    print e[2].definition, len(wordnet.synsets(sentence["noun2"]))
                    print e[0]
                except:
                    print "I got nothing"
                    
    print good, bad